/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.nubotech.gwt.oss.browser.client.ui;

import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.nubotech.gwt.oss.client.OnlineStorageServiceFactory;
import com.nubotech.gwt.oss.client.auth.Credential;
import java.util.Date;

/**
 *
 * @author jonnakkerud
 */
public class GenUrlView extends Composite {
    private DockPanel main;

    public GenUrlView() {
        initWidget(main = new DockPanel());
    }
    
    protected void onLoad() {
        initGui();
    }

    private void initGui() {
        main.setSpacing(10);
        
        Label header = new Label("Create URL");
        header.addStyleName("caption");
        main.add(header, DockPanel.NORTH);
        main.setCellHorizontalAlignment(header, DockPanel.ALIGN_CENTER);
        
        Grid grid = new Grid(5,2);
        grid.setCellSpacing(5);
        main.add(grid, DockPanel.CENTER);
        
        grid.setText(0,0, "account:");
        final TextBox account = new TextBox();
        grid.setWidget(0, 1, account);
        grid.setText(1,0,"s-key:");
        final TextBox pass = new TextBox();
        grid.setWidget(1,1, pass);       
        
        grid.setText(2,0, "bucket:");
        final TextBox bucket = new TextBox();
        grid.setWidget(2, 1, bucket);
        grid.setText(3,0,"key:");
        final TextBox key = new TextBox();
        grid.setWidget(3,1, key);

        grid.setText(4,0,"url:");
        final TextBox url = new TextBox();
        grid.setWidget(4,1, url);
        
        HorizontalPanel buttonsPanel = new HorizontalPanel();
        main.add(buttonsPanel, DockPanel.SOUTH);
        
        Button loginB = new Button("login");
        buttonsPanel.add(loginB);
        loginB.addClickListener(new ClickListener() {
            public void onClick(Widget sender) {
                Credential c = new Credential(account.getText(), pass.getText());
                Date day = new Date(); // now + 24hr
                day.setTime(day.getTime() + 86400000);
                String result = OnlineStorageServiceFactory.getService(c).createGetLink(bucket.getText(), key.getText(), null);
                url.setText(result);
            }
        });
        
        
    }
}
